<template>
    <div class="detail-container">
      <!-- 基本信息 -->
      <div class="info-card">
        <div class="info-item">
          <span class="label">部门：</span>
          <span class="value">{{ detailInfo.departName }}</span>
        </div>
        <div class="info-item">
          <span class="label">申请人：</span>
          <span class="value">{{ detailInfo.applyName }}</span>
        </div>
        <div class="info-item">
          <span class="label">状态：</span>
          <span :class="getStatusClass(detailInfo.applyStatus)">{{ getStatusText(detailInfo.applyStatus) }}</span>
        </div>
        <div class="info-item">
          <span class="label">申请时间：</span>
          <span class="value">{{ detailInfo.applyTime }}</span>
        </div>
        <div class="info-item">
          <span class="label">领取时间：</span>
          <span class="value">{{ detailInfo.receviceTime || '--' }}</span>
        </div>
        <div class="info-item">
          <span class="label">备注：</span>
          <span class="value">{{ detailInfo.remark || '--' }}</span>
        </div>
      </div>
  
      <!-- 物品清单 -->
      <div class="goods-section">
      <div class="section-title">物品列表</div>
      <div class="goods-list">
        <div v-for="(item, index) in list" :key="index" class="goods-item">
          <div class="goods-name">{{ item.goodsName }}</div>
          <div class="goods-detail">
            <div class="amount-info">
              <div>申请数量：{{ item.goodsAmount }} {{ item.goodsUnit }}</div>
              <div>审批数量：{{ item.approvedAmount || item.goodsAmount }} {{ item.goodsUnit }}</div>
            </div>
            <div class="remark">备注：{{ item.remark || '--' }}</div>
          </div>
        </div>
      </div>
    </div>
  
      <!-- 合计金额 -->
      <div class="total-amount">
        合计金额：<span class="price">￥{{ detailInfo.amount }}</span>
      </div>
  
      <!-- 审批进度 -->
      <div class="approval-section" v-if="detailInfo.approvalInfo">
        <div class="section-title">审批进度</div>
        <approval :detail="detailInfo"></approval>
      </div>
  
      <!-- 审批操作区 -->
      <template v-if="taskId && detailInfo.applyStatus == 1">
        <template v-if="!detailInfo.ifCurrentUserApproved">
          <div class="section-title">
            <span class="title-bar"></span>
            审批内容
          </div>
          <div class="approval-input">
            <van-field
              v-model="result"
              type="textarea"
              maxlength="200"
              placeholder="说点什么吧"
              show-word-limit
              rows="4"
            />
          </div>
          <div class="button-group">
            <van-button 
              class="reject-btn" 
              type="danger" 
              :loading="loading"
              @click="handConsent($event, '3')"
            >
              驳回
            </van-button>
            <van-button 
              type="primary" 
              :loading="loading"
              @click="handConsent($event, '2')"
            >
              通过
            </van-button>
          </div>
        </template>
      </template>
    </div>
  </template>
  
  <script>
  import { Toast } from 'vant'
  import { getApplyRecordDetails, handleApproval } from '@/api/goodsRequisitionDetail'

  
  export default {
    name: 'GoodsRequisitionDetail',
  
    data() {
      return {
        detailInfo: {
          departName: '',
          applyName: '',
          applyStatus: '',
          applyTime: '',
          receviceTime: '',
          remark: '',
          amount: '',
          approvalInfo: null
        },
        list: [],
        result: '',
        loading: false,
        taskId: ''
      }
    },
  
    created() {
      const { id = 0 } = this.$route.query
      this.detailId = id
      this.loadDetailData()
    },
  
    methods: {
      loadDetailData() {
        const params = {
          openSessionId: JSON.parse(localStorage.getItem('user'))?.openSessionId,
          applyId: this.detailId
        }
  
        getApplyRecordDetails(params)
          .then(res => {
            if (res.code === 200) {
              const data = res.data
              this.detailInfo = data.apply
              this.list = data.applyDetails
            } else {
              Toast.fail(res.message || '获取详情失败')
            }
          })
          .catch(err => {
            console.error(err)
            Toast.fail('获取详情失败')
          })
      },
  
      handConsent(e, type) {
        if (this.loading) return
        this.loading = true
  
        const params = {
          openSessionId: JSON.parse(localStorage.getItem('user'))?.openSessionId,
          applyId: this.detailId,
          type,
          result: this.result
        }
  
        handleApproval(params)
          .then(res => {
            if (res.code === 200) {
              Toast.success('操作成功')
              this.$router.back()
            } else {
              Toast.fail(res.message)
            }
            this.loading = false
          })
          .catch(err => {
            console.error(err)
            Toast.fail('操作失败')
            this.loading = false
          })
      },
  
      getStatusText(status) {
        const statusMap = {
          1: '待审批',
          4: '待领取',
          6: '已领取',
          5: '已驳回',
          3: '已撤回'
        }
        return statusMap[status] || '--'
      },
  
      getStatusClass(status) {
        const classMap = {
          1: 'status-pending',
          4: 'status-waiting',
          6: 'status-complete',
          5: 'status-reject',
          3: 'status-cancel'
        }
        return classMap[status] || ''
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .detail-container {
    min-height: 100vh;
    background-color: #f5f5f5;
    padding: 12px;
  }
  
  .info-card {
    background: #fff;
    border-radius: 4px;
    padding: 0 15px;
    margin-bottom: 15px;
  }
  
  .info-item {
    display: flex;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid #eee;
    font-size: 14px;
    
    &:last-child {
      border-bottom: none;
    }
  
    .label {
      color: #666;
    }
  
    .value {
      color: #333;
    }
  }
  
  .goods-section {
    background: #fff;
    border-radius: 4px;
    padding: 12px 15px;
    margin-bottom: 15px;
  }
  
  .section-title {
    font-size: 14px;
    color: #333;
    margin-bottom: 12px;
    border-bottom: 1px solid #eee;
    padding-bottom: 12px;
    text-align: left;
  }
  
  .goods-item {
    padding: 12px 0;
    border-bottom: 1px solid #eee;
  
    &:last-child {
      border-bottom: none;
    }
  }
  .goods-name{
    text-align: left;
    margin-bottom: 10px;
  }
  
  .goods-detail {
    color: #666;
    font-size: 14px;
    
    .amount-info {
      display: flex;
      justify-content: space-between;
      margin-bottom: 8px;
    }
  
    .remark {
      color: #999;
      text-align: left;
    }
  }
  
  .price {
    color: #1989fa;
  }
  
  .total-amount {
    background: #fff;
    padding: 12px 15px;
    text-align: right;
    border-radius: 4px;
    margin-bottom: 15px;
  }
  
  .status {
    &-pending { color: #ff8832; }
    &-waiting { color: #4498ff; }
    &-complete { color: #30d147; }
    &-reject { color: #ed5454; }
    &-cancel { color: #cccccc; }
  }
  
  .button-group {
    display: flex;
    justify-content: space-between;
    margin-top: 20px;
    
    .van-button {
      width: 45%;
    }
  }
  </style>